package com.icinfo.cloud.provider.peripheralinterface.xfjdzf.service.impl;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.HttpUtils;
import com.icinfo.cloud.provider.peripheralinterface.xfjdzf.service.IXfjdzfInterfaceService;
import com.icinfo.cloud.provider.punish.api.dto.ApiSimpleCaseDetailInfoCallbackDto;
import com.icinfo.cloud.provider.punish.api.dto.ApiSimpleCaseDetailInfoDto;
import com.icinfo.cloud.provider.punish.api.service.IApiSimpleCaseService;
import com.icinfo.cloud.provider.punish.api.vo.ApiSimpleCaseDetailInfoVo;
import com.icinfo.cloud.provider.punish.ucase.caseregister.dto.CaseHandlePersonInfoDto;
import com.icinfo.framework.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.json.HTTP;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：消防监督执法接口服务实现类
 *
 * @author xiangyuyu
 * @date 2022/8/13
 * @since 1.3.2
 */
@Service
@Slf4j
public class XfjdzfInterfaceServiceImpl implements IXfjdzfInterfaceService {
    @Value("${xfjdzf.returnSimpleCaseUrl}")
    private String returnSimpleCaseUrl;
    @Resource
    private IApiSimpleCaseService iApiSimpleCaseService;

    /**
     * 【V1.3.2】描述：简易处罚信息回流
     *
     * @param dto {@link ApiSimpleCaseDetailInfoCallbackDto}
     * @return Result<String> 回流结果
     * @author xiangyuyu
     * @date 2022/8/11
     * @since 1.3.2
     */
    @Override
    public Result<String> callbackSimpleCaseDetailInfo(ApiSimpleCaseDetailInfoCallbackDto dto) {
        log.info("callbackSimpleCaseDetailInfo start...dto:{}", dto);
        //获取简易案件详情信息
        ApiSimpleCaseDetailInfoDto queryDto = new ApiSimpleCaseDetailInfoDto();
        queryDto.setCaseId(dto.getCaseId());
        queryDto.setDataSource(dto.getDataSource());
        ApiSimpleCaseDetailInfoVo vo = iApiSimpleCaseService.getSimpleCaseDetailInfo(queryDto);
        List<CaseHandlePersonInfoDto> handlePersonInfoList = vo.getHandlePersonInfoList();
        if (ObjectUtils.isEmpty(vo)) {
            Result.error("未匹配到简易处罚详情信息");
        }
        vo.setHandlePersonInfoList(handlePersonInfoList);
        try {
            String jsonObject = JSON.toJSONString(vo);
            log.info("jsonObject:{}",jsonObject);
            String result = HttpUtils.doPostJson(returnSimpleCaseUrl, jsonObject);
            log.info("result:{}",result);
            if (!ObjectUtils.isEmpty(result)) {
                JSONObject resultJO = JSONObject.parseObject(result);
                if (!ObjectUtils.isEmpty(resultJO)) {
                    Object code = resultJO.get(Constant.STRING_CODE_LOWER);
                    if (!ObjectUtils.isEmpty(code) && HttpStatus.HTTP_OK == (int) code) {
                        return Result.success("回调成功");
                    } else {
                        log.error("callbackSimpleCaseDetailInfo 响应code:{},msg:{}",code, resultJO.getString(Constant.STRING_MESSAGE_LOWER));
                        return Result.error("callbackSimpleCaseDetailInfo 响应code:"+code+",msg:"+resultJO.getString(Constant.STRING_MESSAGE_LOWER));
                    }
                } else {
                    log.error("callbackSimpleCaseDetailInfo error(result transform JSONObject)");
                    return Result.error("callbackSimpleCaseDetailInfo error(result transform JSONObject");
                }
            } else {
                log.error("callbackSimpleCaseDetailInfo result:null");
                return Result.error("callbackSimpleCaseDetailInfo result:null");
            }

        } catch (Exception e) {
            log.error("callbackSimpleCaseDetailInfo error:{}", e.getMessage());
            return Result.error("callbackSimpleCaseDetailInfo error:"+e.getMessage());
        }
    }
}
